﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TicTacToe
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private Char?[,] Board = new char?[3,3];
        private Boolean Turn;
        private Boolean GameEnded = false; 
        enum Location
        {
            TopLeft = 1,
            TopMiddle,
            TopRight,
            MiddleLeft,
            MiddleMiddle,
            MiddleRight,
            BottomLeft,
            BottomMiddle,
            BottomRight 
        };
        public Window1()
        {
            InitializeComponent();
        }

           private void Window_MouseDown(object sender, MouseButtonEventArgs e)
           {
               if (GameEnded == false)
               {
                   Point Pos = e.MouseDevice.GetPosition(this);
                   if (Turn)
                   {
                       Turn = !Turn;
                       DrawX(GetThickness(e));
                   }
                   else
                   {
                       Turn = !Turn;
                       DrawO(GetThickness(e));
                   }
               }
           }
                   
           #region Get Thicknesss

           private Thickness? GetThickness(MouseButtonEventArgs e)
           {
               Point Pos = e.MouseDevice.GetPosition(this);
               // * - -  
               // - - - 
               // - - - 
               if (Pos.X < 90 && Pos.Y < 90)
               {

                   if (Board[0, 0] == null)
                   {
                       if (Turn)
                       {
                           Board[0, 0] = 'X';
                       }
                       else
                       {
                           Board[0, 0] = 'Y';
                       }
                           return new Thickness(10, 10, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // -*-
               // ---
               // ---
               else if (Pos.X < 190 && Pos.Y < 90)
               {
                   if (Board[0, 1] == null)
                   {
                       if (Turn)
                       {
                           Board[0, 1] = 'X';
                       }
                       else
                       {
                           Board[0, 1] = 'Y';
                       }
                       return new Thickness(115, 10, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // --*
               // ---
               // ---
               else if (Pos.X > 190 && Pos.Y < 90)
               {
                   if (Board[0, 2] == null)
                   {
                       if (Turn)
                       {
                           Board[0, 2] = 'X';
                       }
                       else
                       {
                           Board[0, 2] = 'Y';
                       }
                       return new Thickness(200, 10, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // ---
               // *--
               // ---
               else if (Pos.X > 0 && Pos.X < 90 && Pos.Y > 90 && Pos.Y < 180)
               {
                   if (Board[1, 0] == null)
                   {
                       if (Turn)
                       {
                           Board[1, 0] = 'X';
                       }
                       else
                       {
                           Board[1, 0] = 'Y';
                       }
                       return new Thickness(10, 90, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // ---
               // -*-
               // ---
               else if (Pos.X > 90 && Pos.X < 180 && Pos.Y > 90 && Pos.Y < 180)
               {
                   if (Board[1, 1] == null)
                   {
                       if (Turn)
                       {
                           Board[1, 1] = 'X';
                       }
                       else
                       {
                           Board[1, 1] = 'Y';
                       }
                       return new Thickness(115, 90, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // ---
               // --*
               // ---
               else if (Pos.X > 180 && Pos.Y > 90 && Pos.Y < 180)
               {
                   if (Board[1, 2] == null)
                   {
                       if (Turn)
                       {
                           Board[1, 2] = 'X';
                       }
                       else
                       {
                           Board[1, 2] = 'Y';
                       }
                       return new Thickness(200, 90, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // ---
               // ---
               // *--
               else if (Pos.X < 90 && Pos.Y > 180)
               {
                   if (Board[2, 0] == null)
                   {
                       if (Turn)
                       {
                           Board[2, 0] = 'X';
                       }
                       else
                       {
                           Board[2, 0] = 'Y';
                       }
                       return new Thickness(10, 180, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               // ---
               // ---
               // -*-
               else if (Pos.X > 90 && Pos.X < 180 && Pos.Y > 180)
               {
                   if (Board[2, 1] == null)
                   {
                       if (Turn)
                       {
                           Board[2, 1] = 'X';
                       }
                       else
                       {
                           Board[2, 1] = 'Y';
                       }
                       return new Thickness(115, 180, 0, 0);
                   }
                   else
                   {
                       return null;
                   }

               }
               // ---
               // ---
               // --*
               else if (Pos.X > 180 && Pos.Y > 180)
               {
                   if (Board[2, 2] == null)
                   {
                       if (Turn)
                       {
                           Board[2, 2] = 'X';
                       }
                       else
                       {
                           Board[2, 2] = 'Y';
                       }
                       return new Thickness(200, 180, 0, 0);
                   }
                   else
                   {
                       return null;
                   }
               }
               else
                   return null; 
           }
           #endregion 

           private void CheckEndGame()
           {
               ///***
               ///---
               ///---
               if ((Board[0, 0] == Board[0, 1]) && (Board[0, 1] == Board[0, 2]) && (Board[0, 0] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 0;
                   Line.X2 = 260;
                   Line.Y1 = 30;
                   Line.Y2 = 30;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }
               ///---
               ///***
               ///---
               if ((Board[1, 0] == Board[1, 1]) && (Board[1, 0] == Board[1, 2]) && (Board[1, 0] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 4;
                   Line.X2 = 258;
                   Line.Y1 = 105;
                   Line.Y2 = 105;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }

               ///---
               ///---
               ///***
               if ((Board[2, 0] == Board[2, 1]) && (Board[2, 0] == Board[2, 2]) && (Board[2, 0] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 10;
                   Line.X2 = 264;
                   Line.Y1 = 200;
                   Line.Y2 = 200;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }

               ///*--
               ///*--
               ///*--
               if ((Board[0, 0] == Board[1, 0]) && (Board[1, 0] == Board[2, 0]) && (Board[0,0] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 30;
                   Line.X2 = 30;
                   Line.Y1 = 10;
                   Line.Y2 = 250;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }
               ///-*-
               ///-*-
               ///-*-
               if ((Board[0, 1] == Board[1, 1]) && (Board[1, 1] == Board[2, 1]) && (Board[0, 1] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 130;
                   Line.X2 = 130;
                   Line.Y1 = 10;
                   Line.Y2 = 236;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }

               ///--*
               ///--*
               ///--*
               if ((Board[0, 2] == Board[1, 2]) && (Board[0, 2] == Board[2, 2]) && (Board[0, 2] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 215;
                   Line.X2 = 2150;
                   Line.Y1 = 10;
                   Line.Y2 = 250;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }

               ///*--
               ///-*-
               ///--*
               if ((Board[0, 0] == Board[1, 1]) && (Board[0, 0] == Board[2, 2]) && (Board[0, 0] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 23;
                   Line.X2 = 270;
                   Line.Y1 = 10;
                   Line.Y2 = 250;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }

               ///--*
               ///-*-
               ///*--
               if ((Board[0, 2] == Board[2, 1]) && (Board[0, 2] == Board[2, 0]) && (Board[0, 2] != null))
               {
                   Line Line = new Line();
                   Line.Stroke = Brushes.Black;
                   Line.Margin = new Thickness(10, 10, 0, 0);
                   Line.X1 = 50;
                   Line.X2 = 50;
                   Line.Y1 = 10;
                   Line.Y2 = 119;
                   Line.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line);
                   GameEnded = true;
               }
           }

          private void DrawX(Thickness? T)
           {
               if (T.HasValue)
               {
                   //  Thickness T = new Thickness(100, 100, 100, 100);
                   Line Line1 = new Line();
                   Line Line2 = new Line();

                   Line1.Stroke = Brushes.Black;
                   Line1.Margin = T.Value;
                   Line1.X1 = 10;
                   Line1.Y1 = 10;
                   Line1.X2 = 50;
                   Line1.Y2 = 50;
                   Line1.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line1);

                   Line2.Margin = T.Value;
                   Line2.Stroke = Brushes.Black;
                   Line2.X1 = 50;
                   Line2.Y1 = 10;
                   Line2.X2 = 10;
                   Line2.Y2 = 50;
                   Line2.StrokeThickness = 1;
                   GridBoard.Children.Insert(0, Line2);
                   CheckEndGame();
               }
           }

           private void DrawO(Thickness? T)
           {
               if (T.HasValue)
               {
                   Ellipse e = new Ellipse();
                   e.Margin = T.Value;
                   e.Height = 50;
                   e.Width = 50;
                   e.VerticalAlignment = VerticalAlignment.Top;
                   e.HorizontalAlignment = HorizontalAlignment.Left;
                   e.Stroke = Brushes.Black;
                   GridBoard.Children.Insert(0, e);
                   CheckEndGame();
               }
           }

           private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
           {

               Point Pos = e.MouseDevice.GetPosition(this);
           }

           private void ResetBoard()
           {
               Board[0, 0] = null;
               Board[0, 1] = null;
               Board[0, 2] = null;
               Board[1, 0] = null;
               Board[1, 1] = null;
               Board[1, 2] = null;
               Board[2, 0] = null;
               Board[2, 1] = null;
               Board[2, 2] = null;
               GridBoard.Children.Clear();
               GameEnded = false;
           }
           
           private void btnReset_Click(object sender, RoutedEventArgs e)
           {
               ResetBoard(); 
           }
    }
}
